package ru.home.shoppinglist;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {
	private static final String LOG_TAG = "MainActivity";
	private static final int MENU_EDIT_CATEGORY = 0;
	DBHelper dbHelper;
	SimpleCursorAdapter scAdapter;
	Cursor cursor;
	ListView lvData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		dbHelper = new DBHelper(this);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		cursor = db.rawQuery(
				"select id _id,name||id name from Classification order by name",
				new String[0]);
		// cursor=db.query("Classification", null, null, null, null, null,
		// null);
		startManagingCursor(cursor);
		// формируем столбцы сопоставления
		String[] from = new String[] { "name" };
		int[] to = new int[] { R.id.tvText };
		
		//registerForContextMenu(findViewById(R.id.lvClassifications));

		scAdapter = new SimpleCursorAdapter(this, R.layout.classification_item,
				cursor, from, to);
		lvData = (ListView) findViewById(R.id.lvClassifications);
		//lvData.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		// android.R.layout.simple_list_item_single_choice
		lvData.setAdapter(scAdapter);	
		lvData.setOnCreateContextMenuListener(this);		
		lvData.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int arg2, long id) {
				Intent intent = new Intent(arg0.getContext(), GoodsActivity.class);
		    	intent.putExtra("category_id", id);
		    	startActivity(intent);
			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onAddRecordClick(View view) {
		Intent intent = new Intent(this, AddClassificationActivity.class);
		// Intent intent = new
		// Intent("ru.home.shoppinglist.add_classification");

		startActivity(intent);
	}

	public void onSelectCategory(View view) {

	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		 AdapterContextMenuInfo aMenuInfo = (AdapterContextMenuInfo) menuInfo;
		super.onCreateContextMenu(menu, v, menuInfo);
		switch (v.getId()) {
	    case R.id.lvClassifications:
	      menu.add(0, MENU_EDIT_CATEGORY, 0, "Edit Category");
	      break;	    
	    }
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
	    case MENU_EDIT_CATEGORY:
	    	Intent intent = new Intent(this, AddClassificationActivity.class);
	    	intent.putExtra("category_id", ((AdapterContextMenuInfo)item.getMenuInfo()).id);
	    	startActivity(intent);
	      break;
		}
		return super.onContextItemSelected(item);
	}
}
